package cn.cjj.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.cjj.gulimall.product.dao.AttrAttrgroupRelationDao;
import cn.cjj.gulimall.product.entity.AttrAttrgroupRelationEntity;
import cn.cjj.gulimall.product.entity.AttrEntity;
import cn.cjj.gulimall.product.service.AttrAttrgroupRelationService;
import cn.cjj.gulimall.product.service.AttrService;
import cn.cjj.gulimall.product.service.CategoryService;
import cn.cjj.gulimall.product.vo.AttrGroupRelationVo;
import cn.cjj.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.cjj.gulimall.product.entity.AttrGroupEntity;
import cn.cjj.gulimall.product.service.AttrGroupService;
import cn.cjj.common.utils.PageUtils;
import cn.cjj.common.utils.R;



/**
 * 属性分组
 *
 * @author cjj
 * @email cjj@gmail.com
 * @date 2021-05-27 20:45:15
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;


    // /product/attrgroup/{catelogId}/withattr
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        System.out.println("/{catelogId}/withattr");
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data", attrGroupWithAttrsVos);
    }


    /**
     * /product/attrgroup/attr/relation
     * [{
     *   "attrGroupId": 0, //分组id
     *   "attrId": 0, //属性id
     * }]
     */
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos) {

        relationService.saveBath(attrGroupRelationVos);

        return R.ok();
    }


    // /product/attrgroup/{attrgroupId}/noattr/relation
    // {
    //   page: 1,//当前页码
    //   limit: 10,//每页记录数
    //   sidx: 'id',//排序字段
    //   order: 'asc/desc',//排序方式
    //   key: '华为'//检索关键字
    //}
    @RequestMapping("{attrgroupId}/noattr/relation")
    public R noattrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrService.getNoRelationAttr(params, attrgroupId);

        return R.ok().put("page", pageUtils);
    }

    // /product/attrgroup/attr/relation/delete
    // [{"attrId":1,"attrGroupId":2}]
    @RequestMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos) {
        attrService.deleteRelation(attrGroupRelationVos);

        return R.ok();
    }

    // http://localhost:88/api/product/attrgroup/1/attr/relation?t=1623810030906
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{categoryId}")
    public R list2(@RequestParam Map<String, Object> params, @PathVariable Long categoryId){
        PageUtils page = attrGroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
